package com.atguigu.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.ware.feign.MemberFeignService;
import com.atguigu.ware.vo.FareVo;
import com.atguigu.ware.vo.MemberAddressVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.ware.dao.WareInfoDao;
import com.atguigu.ware.entity.WareInfoEntity;
import com.atguigu.ware.service.WareInfoService;

import javax.annotation.Resource;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

  @Resource
  MemberFeignService memberFeignService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<WareInfoEntity> page = this.page(
      new Query<WareInfoEntity>().getPage(params),
      new QueryWrapper<WareInfoEntity>()
    );

    return new PageUtils(page);
  }

  @Override
  public FareVo getFare(Long addrId) {
    FareVo fareVo = new FareVo();
    R<MemberAddressVo> info = memberFeignService.info(addrId);
    if (info.getCode() == 0) {
      MemberAddressVo address = info.getData("memberReceiveAddress", new TypeReference<MemberAddressVo>() {
      });
      fareVo.setAddress(address);
      String phone = address.getPhone();
      //取电话号的最后两位作为邮费
      String fare = phone.substring(phone.length() - 2, phone.length());
      fareVo.setFare(new BigDecimal(fare));
    }
    return fareVo;
  }


}
